package cn.imvc.app.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.imvc.app.entities.Area;
import cn.imvc.app.entities.Member;
import cn.imvc.app.models.AreaModel;
import cn.imvc.app.models.MemberModel;
import cn.imvc.util.JSONUtils;

@Controller
@RequestMapping("/area")
public class AreaControllerImpl implements AreaController {

	@Autowired
	AreaModel areaModel;

	@Autowired
	MemberModel memberModel;

	@RequestMapping("/getAreas.html")
	@ResponseBody
	public String getAreas(String token, int parentId) {
		Member member = memberModel.getLoginMember(token);
		if (member != null) {
			List<Area> result = areaModel.getAreaList(parentId);
			return JSONUtils.toJSONStringWithoutCollectionProperty(result);
		} else {
			return "redirect:/member/login.html";
		}
	}

	@RequestMapping("/areaToJson.html")
	@ResponseBody
	public String areaToJson() {
		return areaModel.areaToJson();
	}
}
